package com.hdu.hot100.list;

import java.util.HashMap;
import java.util.Map;

public class lc138 {

    class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }


    class Solution {
        public Node copyRandomList(Node head) {

            Map<Node, Node> nodeRel = new HashMap<>();
            Node cur = head;
            while (cur != null)
            {
                Node newNode = new Node(cur.val);
                nodeRel.put(cur, newNode);
                cur = cur.next;
            }

            cur = head;

            while (cur != null)
            {
                Node newNode = nodeRel.get(cur);
                if (cur.next != null)
                {
                    newNode.next = nodeRel.get(cur.next);
                }
                if (cur.random != null)
                {
                    newNode.random = nodeRel.get(cur.random);
                }
                cur = cur.next;
            }

            return nodeRel.get(head);
        }
    }
}
